

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import services.FilmsService;

import com.xyz.Film;

/**
 * Servlet implementation class FilmFilter
 */
@WebServlet("/jsp/years/*")
public class FilmFilterYears extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FilmFilterYears() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String path=request.getPathTranslated();
		String filmYears="";
		int i=path.length()-1;
		while(i>=0 && path.charAt(i)!=92){
			filmYears+=path.charAt(i--);
		}
		//pw.println(filmName);
		i=0;
		filmYears=new StringBuilder(filmYears).reverse().toString();
		String buf="";
		while(i+3<filmYears.length() && (filmYears.charAt(i)!='.' || filmYears.charAt(i+1)!='j' || filmYears.charAt(i+2)!='s' || filmYears.charAt(i+3)!='p')){
			i++;
		}
		for (int j=0; j<i; j++){
			buf+=filmYears.charAt(j);
		}
		filmYears=buf;
		
		List <Film> films=new ArrayList<Film>();
		
		if (filmYears.equals("1")){
			films=FilmsService.getInstance().getFilmsByYear(2012, 2014);
		}else if (filmYears.equals("2")){
			films=FilmsService.getInstance().getFilmsByYear(2009, 2013);
		}else if (filmYears.equals("3")){
			films=FilmsService.getInstance().getFilmsByYear(2004, 2010);
		}else if (filmYears.equals("4")){
			films=FilmsService.getInstance().getFilmsByYear(1999, 2005);
		}else if (filmYears.equals("5")){
			films=FilmsService.getInstance().getFilmsByYear(1989, 2000);
		}else if (filmYears.equals("6")){
			films=FilmsService.getInstance().getFilmsByYear(1979, 1990);
		}else if (filmYears.equals("7")){
			films=FilmsService.getInstance().getFilmsByYear(0, 1980);
		}
		
		request.setAttribute("filmList", films);
		request.setCharacterEncoding("UTF-8");
		RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/jsp/filteredFilms.jsp");
		view.forward(request, response);
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
